package com.yl.chainStore.control.user

import com.alibaba.fastjson.JSONObject
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.yl.chainStore.control.BaseCtrl
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.entity.CompanyUser
import com.yl.users.entity.CompanyUserTodo
import com.yl.users.services.ICompanyUserService
import com.yl.users.services.ICompanyUserTodoService
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.*

/**
 * 代办事项
 */
@RestController
@RequestMapping("company/user/todo")
open class CompanyUserTodoController(

    private val iCompanyUserTodoService: ICompanyUserTodoService,
    private val iCompanyUserService: ICompanyUserService
):BaseCtrl() {

    @PostMapping("page")
    @Transactional
    open fun page(@RequestBody body: JSONObject): ResponseBody {
        val todo = CompanyUserTodo(
            title = body.getString("title"),
            content=body.getString("content")
        )
        val page = Page<CompanyUserTodo>(body.getLong("current") ?: 1L, body.getLong("size") ?: 10L)
        return returnSuccess(iCompanyUserService.page(page,todo))
    }

    @GetMapping("detail/{id}")
    @Transactional
    open fun detail(@PathVariable id: Long): ResponseBody {
        return returnSuccess(iCompanyUserTodoService.detail(id))
    }

    @PostMapping("add")
    @Transactional
    open fun add(@RequestBody body: CompanyUserTodo): ResponseBody {
        if(body.userId == null){
            body.userId = getContext().userId
        }
        return iCompanyUserService.addTodo(body)
    }

    @PostMapping("complete/{id}")
    @Transactional
    open fun complete(@PathVariable id: Long): ResponseBody{
        return iCompanyUserTodoService.updateToComplete(id)
    }

    @PostMapping("del/{id}")
    @Transactional
    open fun del(@PathVariable id: Long): ResponseBody {
        return iCompanyUserTodoService.del(id)
    }





}
